/**
 *Project: jh-framework
 *File: package cn.xkshow.io.ByteArrayDataSource.java 
 *Author: admin@xkshow.cn with the copyrite
 ****************************************************************
 *   VERSION       DATE            BY       CHANGE/COMMENT
 * --------------------------------------------------------------
 *    1.0        2013-1-27        xkshow.cn         create
 ****************************************************************
 *note:
 ***************************************************************/ 
package cn.xkshow.io;

import java.io.*;
import javax.activation.DataSource;

public class ByteArrayDataSource implements DataSource{
	private byte data[];
    private String type;
    
    public ByteArrayDataSource(InputStream is, String type)
    {
        this.type = type;
        try
        {
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            int ch;
            while((ch = is.read()) != -1) 
                os.write(ch);
            data = os.toByteArray();
        }
        catch(IOException ioexception) { }
    }

    public ByteArrayDataSource(byte data[], String type)
    {
        this.data = data;
        this.type = type;
    }

    public ByteArrayDataSource(String data, String enc, String type)
    {
        try
        {
            this.data = data.getBytes(enc);
        }
        catch(UnsupportedEncodingException unsupportedencodingexception) { }
        this.type = type;
    }

    public InputStream getInputStream()
        throws IOException
    {
        if(data == null)
            throw new IOException("no data");
        else
            return new ByteArrayInputStream(data);
    }

    public OutputStream getOutputStream()
        throws IOException
    {
        throw new IOException("cannot do this");
    }

    public String getContentType()
    {
        return type;
    }

    public String getName()
    {
        return "dummy";
    }
}

